package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import ControlLayer.*;//The TUILayer needs to know the ControlLayer

/**
 * 
 * @Author (Alexander Hvidberg, Dennis Kirkebak & Thomas Kulmbach)
 * @Team (Group 1)
 * @Version (08/11/13)
 * 
 */
public class AddressBookUI
{   

    private AddressCtr addrCtr;//reference to the ControlLayer
    
    /**
     * Constructor for the class AddressBookUI()
     */
    public AddressBookUI()
    {
       addrCtr = AddressCtr.getInstance();
    }
    
    /**
     * Starts the address book menu
     */
    public void start()
    {
       
        addressMenu();
    }
    
    /**
     * Takes the input "choise" from the method writeAddressBookMenu() and uses it
     * to initialize the chosen method.
     */
    public void addressMenu()
    {
       boolean exit = false;
       while(!exit)//that is: while exit is false
       {
          int choise = writeAddressBookMenu(); 
          if(choise == 1)
          {   //show person
              getPersonByPhone();
          }
          else{
              if(choise == 2){
                    createPerson();
              }  
              else{
                  if(choise == 3){
                      deletePerson();
                  }  
                  else{
                      if(choise == 4){
                          updatePerson();
                      }
                      else{
                          if(choise == 5){
                             listAllPersons();
                            }
                          else{
                             if(choise == 6){
                                 clearScreen();
                                }
                                else{
                              exit = true;
                           }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
        }//end else
            
       }//end while
    }
    /**
     * Prints out the address book menu
     * Also records the entered choice
     * 
     * @return int choic - The chosen menu point
     */
    private int writeAddressBookMenu()
    {      // makes an object keyboard to read input from the screen
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\n \n *** AddressBook Menu ***");
            System.out.println(" (1) Show information about person");
            System.out.println(" (2) Add person");
            System.out.println(" (3) Delete person");
            System.out.println(" (4) Update person");
            System.out.println(" (5) List all persons");
            System.out.println(" (6) Clear Screen");
            System.out.println(" (7) Return");
            System.out.print("\n Make your choice: ");
            
            int choise = keyboard.nextInt();
            return choise;
        }
    
        /**
         * Clears the screen of text
         */
    private void clearScreen(){
        System.out.println("\f");
    }
    //the method reads id for a person and return
    // 
    /**
     * A method to record an input of the persons ID
     * @return int id - The persons ID number
     */
    private int inputPersonsID()
    {   
        boolean ok = false;
        int id = 0;
        while(!ok)
        {
            // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Id:  ");
           try{
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("\f The input must be a number - Try again");
             String input = keyboard.nextLine();
           }
        }//end while
        return id;
    }
    
    /**
     * A method to record an input of the persons name
     * @return String name - The persons name
     */
    private String inputName()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Name:  ");
         String name = keyboard.nextLine();
         return name;
    }
   
    /**
     * A method to record an input of the persons postal code
     * @return String postalCode - The persons postalCode
     */
    private String inputPostalCode()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Postal Code:  ");
         String code = keyboard.nextLine();
         return code;
    }
    /**
     * A method to record an input of the persons city
     * @return String city - The persons city
     */
    private String inputCity()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write City:  ");
         String city = keyboard.nextLine();
         return city;
    } 
    /**
     * A method to record an input of the persons phone number
     * @return String phoneNumber - The persons phone number
     */
    private String inputPhone()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Phone Number:  ");
         String phone = keyboard.nextLine();
         return phone;
    } 
    
    /**
     * A method to record an input of the persons address
     * @return String address - The persons address
     */
    private String  inputAddress()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Address: ");
        String address = keyboard.nextLine();
        return address;
    }
    
    /**
     * Prints a text line including an id number
     * @return int id - The id number
     */
    private void showId(int id)
    {
        System.out.println("ID of the person: " + id);
    }
    /**
     * Prints a text line including a name
     * @return String name - The name
     */
    private void showName(String name)
    { 
        System.out.println("The name of the persone: " + name);
    }
    /**
     * Prints a text line including an address
     * @return String address - The address
     */
    private void showAddress(String address)
    { 
        System.out.println("The address of the person: " + address);
    }
    /**
     * Takes an input of a persons ID and prints out the information on the person
     */
    private void getPerson()
    {//is to be changed when the address book controller is completed
        int id = inputPersonsID();
        System.out.println("\f" + addrCtr.getPerson(id));
    }   
    

    /**
     * Takes an input of a persons phone number and prints the informaiton on the person
     */
    private void getPersonByPhone(){
        String phone = inputPhone();
        if(addrCtr.getPersonByPhone(phone)==null){
            System.out.println("\f The phone number was not found!");
        }
        else{
        System.out.println("\f" + addrCtr.getPersonByPhone(phone));
      }
    }
     

    /**
     * Takes inputs of the persons name, address, postalcode, city and phone, and creates the person.
     * Also prints out a message confirming that the person has been created
     */
    private void createPerson(){
        clearScreen();
        String name = inputName();
        String address = inputAddress();
        String postalCode = inputPostalCode();
        String city = inputCity();
        String phoneNumber = inputPhone();
        addrCtr.createPerson(name, address, postalCode, city, phoneNumber);
    }
    
    /**
     * Takes an input of a persons id, and deletes that person.
     * Prints out a message confirming that the person has been created
     */
    private void deletePerson(){
        int id = inputPersonsID();
        if(addrCtr.getPersonById(id)==null){
            System.out.println("\f The person does not exist!");
        }
        else if(addrCtr.getPersonById(id)!=null){
            addrCtr.deletePerson(id);
            System.out.println("\f The person has been deleted.\n");
        }
    }
    

    /**
     * Takes inputs of the persons id, name, address, postalcode, city and phone, and updates the 
     * information on the person
     * Also prints out a message confirming that the person has been updated
     */
    private void updatePerson(){
        int id = inputPersonsID();
        String name = inputName();
        String address = inputAddress();
        String postalCode = inputPostalCode();
        String city = inputCity();
        String phoneNumber = inputPhone();
        addrCtr.updatePerson(id, name, address, postalCode, city, phoneNumber);
        System.out.println("\f The person has been updated.\n");
    }
    /**
     * Prints out a list of all the persons in the system
     */
    private void listAllPersons(){
        System.out.println("\f" + addrCtr.printDetailList());
        
    }
}
            
        
